package com.coc.ofhibernate.controller;

import com.coc.ofhibernate.entity.database1.User;
import com.coc.ofhibernate.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping (path = "list", method = RequestMethod.GET)
    public List<User> getUserList(){
        return userService.listAll();
    }


    @RequestMapping ( path = "insert" , method = RequestMethod.GET)
    public String insertUser () {
        User user = new User();
      //  user.setId(2);
        user.setName("kangmjs");
        user.setPassword("Aa123454t");
        user.setAddress("zhonghua shanghai");

        logger.info("接受数据成功 " + user.toString());

        userService.insertUser(user);

        return user.toString();
    }

    @RequestMapping ( path = "insert2" , method = RequestMethod.GET)
    public String insertUser2 () {
        User user = new User();
        //  user.setId(2);
        user.setName("kangmjs");
        user.setPassword("Aa123454t");
        user.setAddress("zhonghua shanghai");

        logger.info("接受数据成功 " + user.toString());

        userService.insertUser(user);

        return user.toString();
    }

    @RequestMapping ( path = "getUser" , method = RequestMethod.GET)
    public User getUser () {
        return userService.getUserById(2);
    }

    @RequestMapping ( path = "getUserList" , method = RequestMethod.GET)
    public List<User> getUserList1 () {
        return userService.getUserListByName("kangmjs");
    }



}
